*** One-Child Policy, Marriage Distortion, and Welfare Loss ***
*** by Wei Huang, Yinghao Pan and Yi Zhou ***

clear all 
set more off
set scheme s1color
cap log close 
graph set window fontface "Times New Roman"

set niceness 10
gl MAIN = "F:\Dropbox"

gl path1 = "$MAIN/Census"
gl path2 = "$MAIN/Marriage/Working_data"
gl path3 = "$MAIN/Marriage/Restat/dataverse_files/Results" 
gl path4 = "$MAIN/Marriage/Restat/dataverse_files/Graphics" 


**** Appendix Table A6 ****
use "$path2/unmarried", clear
keep if relation == 0 | relation == 1 | relation == 2 | relation == 6 


merge 1:1 wave area hhid name relation using "$path2/min_marr_policy", keepusing(marry_1 marry_2 marry_3)


drop if mi(hktype)
replace urban = hktype == 1 
su age 

cap gen female = 1-male
cap gen han = 1- minority 

replace unmarried = unmarried * 100 
gen unmarr_25 = marr_age > 25 if !mi(marr_age)
replace unmarr_25 = 1 if unmarried == 100 
replace unmarr_25 = unmarr_25 *100 

gen urban_hk = hktype == 2 
gen senior = educ >= 4

egen clsid = group(prov_hk year_birth)


gen marry_9 = unmarried == 0 & (mi(marry_1) & mi(marry_2) & mi( marry_3))

recode marry_1 marry_2 marry_3 (. = 0) 

gen HH = marry_1 * 100 
gen HM = marry_2 * 100 
gen MM = marry_3 * 100 
gen Other = marry_9 * 100 
gen HOMO = HH + MM
su HOMO

keep if age <= 55 
keep if age > 25
keep if year_birth <= 1980 
keep if Other == 0 
tab maritus

keep if sex == 2 // women 

egen n_birth = rowtotal(numbirm numbirf)
replace n_birth = . if age > 45
cap drop maritus
gen maritus = 0 if HOMO == 100
replace maritus = 1 if unmarried == 100
replace maritus = 2 if HM == 100 

gl CONTROL = " year_birth sex#age urban_hk prov_hk##c.(year_birth ) "
replace HOMO = 1 if HOMO == 100
replace unmarried = 1 if unmarried == 100
replace HM = 1 if HM == 100


cap erase $path3/birth_decomp.xls
cap erase $path3/birth_decomp.txt
foreach sample in  "n_birth != ."{
foreach gender in "1" {
foreach y_var in "n_birth" {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" {
foreach age in "16_25" {
preserve 
keep if `sample' & `policy' & `gender' & `eth'


reghdfe `y_var' fine_`age' unmarried HM  [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/birth_decomp.xls", append bracket addstat(Y, `r(mean)') ///
ctit(`sample' &`y_var' & `policy' & `gender' & `eth')   dec(3)

restore


}
}
}
}
}
}




foreach sample in  "n_birth != ."{
foreach gender in "1" {
foreach y_var in "unmarried" "HM" {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" {
foreach age in "16_25" {
preserve 
keep if `sample' & `policy' & `gender' & `eth'


reghdfe `y_var' fine_`age'  [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/birth_decomp.xls", append bracket addstat(Y, `r(mean)') ///
ctit(`sample' &`y_var' & `policy' & `gender' & `eth')   dec(3)


restore


}
}
}
}
}
}

reghdfe n_birth fine_16_25 if han &n_birth != . [aw = aw], a($CONTROL)  cluster(prov_hk)
sca birth = _b[fine_16_25]

reghdfe n_birth fine_16_25 unmarried HM if han&n_birth != . [aw = aw], a($CONTROL)  cluster(prov_hk)
sca mechanism_un = _b[unmarried]
sca mechanism_HM = _b[HM]


reghdfe unmarried fine_16_25  if han&n_birth != . [aw = aw], a($CONTROL)  cluster(prov_hk)
sca un = _b[fine_16_25]

reghdfe HM fine_16_25  if han&n_birth != . [aw = aw], a($CONTROL)  cluster(prov_hk)
sca HM1 = _b[fine_16_25]

sca power_all_un = mechanism_un* un /birth * 100 
loc power_all_un : di %3.1f power_all_un

sca power_all_HM = mechanism_HM* HM1 /birth * 100 
loc power_all_HM : di %3.1f power_all_HM

** Pref
reghdfe n_birth fine_16_25 if han &n_birth != . & mino_policy == 1 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca birth_pref = _b[fine_16_25]

reghdfe n_birth fine_16_25 unmarried HM if han &n_birth != . & mino_policy == 1 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca mechanism_pref_un = _b[unmarried]
sca mechanism_pref_HM = _b[HM]


reghdfe unmarried fine_16_25  if han &n_birth != .& mino_policy == 1 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca unmarried_pref = _b[fine_16_25]

reghdfe HM fine_16_25  if han &n_birth != .& mino_policy == 1 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca HM_pref = _b[fine_16_25]

sca power_pref_un = mechanism_pref_un* unmarried_pref /birth_pref * 100 
loc power_pref_un : di %3.1f power_pref_un

sca power_pref_HM = mechanism_pref_HM* HM_pref /birth_pref * 100 
loc power_pref_HM : di %3.1f power_pref_HM

** Non-pref
reghdfe n_birth fine_16_25 if han &n_birth != .& mino_policy == 0 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca birth_no = _b[fine_16_25]

reghdfe n_birth fine_16_25 unmarried HM if han &n_birth != .& mino_policy == 0 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca mechanism_no_un = _b[unmarried]
sca mechanism_no_HM = _b[HM]


reghdfe unmarried fine_16_25  if han &n_birth != .& mino_policy == 0 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca unmarried_no = _b[fine_16_25]

reghdfe HM fine_16_25  if han &n_birth != .& mino_policy == 0 [aw = aw], a($CONTROL)  cluster(prov_hk)
sca HM_no = _b[fine_16_25]


sca power_no_un = mechanism_no_un* unmarried_no /birth_no * 100 
loc power_no_un : di %3.1f power_no_un
sca power_no_HM = mechanism_no_HM* HM_no /birth_no * 100 
loc power_no_HM : di %3.1f power_no_HM


**** Generate Matrics for Decomposition
mat dec = (power_all_un, power_all_HM\ power_pref_un, power_pref_HM \ power_no_un, power_no_HM)

putexcel set $path3\decomposition , replace
putexcel A1 = matrix(dec)


